package com.leiduoduo.oms.controller;

import com.leiduoduo.oms.annotation.PassToken;
import com.leiduoduo.oms.pojo.Restaurant;
import com.leiduoduo.oms.pojo.Table;
import com.leiduoduo.oms.service.RestaurantService;
import com.leiduoduo.oms.service.TableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class TableController {
    @Autowired
    TableService tableService;
    @Autowired
    RestaurantService restaurantService;

    @PostMapping("/addTable")
    @PassToken
    public boolean addTable(Table table){
        return  tableService.addTable(table);
    }
    @PostMapping("/delTable")
    @PassToken
    public boolean delTable(Table table){
        return tableService.delTable(table);
    }
    @GetMapping("/upTable")
    @PassToken
    public boolean upTable(Table table){
        return tableService.upTable(table);
    }
    @GetMapping("/getTableById/{id}")
    @PassToken
    public Table getTableById(@PathVariable("id") int id){
        return tableService.getTableById(id);
    }
    @GetMapping("/getTableList")
    @PassToken
    public List<Table> getTableList(){
        return tableService.getTableList();
    }
    @GetMapping("/getRestaurantTable")
    @PassToken
    public Map<String, Object> getRestaurantTable(int tableId) {
        Map<String, Object> result = new HashMap<>();
        Table table = tableService.getTableById(tableId);
        if (null != table) {
            Map<String, Object> data = new HashMap<>();
            Restaurant restaurant = restaurantService.getRestaurantById(table.getRestaurant_id());
            data.put("table",table);
            data.put("restaurant",restaurant);
            result.put("data",data);
            result.put("errorcode",0);
            result.put("message","seccuss");
        }else {
            result.put("data",null);
            result.put("errorcode",404);
            result.put("message","该桌不存在");
        }
        return result;
    }
    @PostMapping("/upTableState")
    @PassToken
    public Map<String, Object> upTableState(Table table){
        Map<String,Object> result = new HashMap<>();
        if (tableService.upTableState(table)){
            result.put("msg","设置成功");
            result.put("errorcode",0);
        } else {
            result.put("msg","设置失败");
            result.put("errorcode",0);
        }
        return result;
    }
}
